/*
题目链接:https://leetcode.cn/problems/count-the-number-of-fair-pairs/description/?envType=daily-question&envId=2025-04-19
*/

//题解代码:
class Solution {
public:
    #define ll long long
    long long countFairPairs(vector<int>& nums, int lower, int upper) {
        ll ans = 0;
        int n = nums.size();
        sort(nums.begin(),nums.end());
        for(int i=1;i<n;++i){
            int j1 = lower_bound(nums.begin(),nums.begin()+i,lower-nums[i]) - nums.begin();
            int j2 = upper_bound(nums.begin(),nums.begin()+i,upper-nums[i]) - nums.begin();
            ans += j2-j1;
        }
        return ans;
    }
};
